import apiClient from "../apiClient";

export type ConfigGroup = string;

interface ConfigSetting {
	table?: string;
	conditions?: string;
	key?: string;
	value?: string;
}

export interface ConfigItem {
	id: number;
	name: string;
	group: ConfigGroup;
	title: string;
	tip: string;
	type: string;
	visible: string;
	value: string;
	content: string;
	rule: string;
	extend: string;
	setting: ConfigSetting | null;
	extend_html: string;
}

enum Api {
	GetGroups = "/general/config/getGroups",
	GetConfigByGroup = "/general/config/getConfigByGroup",
	SaveConfig = "/general/config/save",
}

class ConfigService {
	// 获取配置组列表
	getGroups() {
		return apiClient.get<ConfigGroup[]>({
			url: Api.GetGroups,
		});
	}

	// 获取指定组的配置项
	getConfigByGroup(group: ConfigGroup) {
		return apiClient.get<ConfigItem[]>({
			url: Api.GetConfigByGroup,
			params: { group },
		});
	}

	// 保存配置
	saveConfig(group: ConfigGroup, configs: Record<string, any>) {
		return apiClient.post({
			url: Api.SaveConfig,
			data: configs,
		});
	}
}

export const configService = new ConfigService();
